<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QSignalMapper Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">&#160;&#160;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QSignalMapper Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1><p>The QSignalMapper class bundles signals from identifiable
senders. <a href="#details">More...</a></p>

<p>Inherits <a href="qobject.html">QObject</a>.</p><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qsignalmapper.html#QSignalMapper">__init__</a></b> (<i>self</i>, QObject&#160;<i>parent</i>&#160;=&#160;None)</li><li><div class="fn" /><b><a href="qsignalmapper.html#map">map</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsignalmapper.html#map-2">map</a></b> (<i>self</i>, QObject&#160;<i>sender</i>)</li><li><div class="fn" />QObject <b><a href="qsignalmapper.html#mapping">mapping</a></b> (<i>self</i>, int&#160;<i>id</i>)</li><li><div class="fn" />QObject <b><a href="qsignalmapper.html#mapping-2">mapping</a></b> (<i>self</i>, QString&#160;<i>text</i>)</li><li><div class="fn" />QObject <b><a href="qsignalmapper.html#mapping-3">mapping</a></b> (<i>self</i>, QWidget&#160;<i>widget</i>)</li><li><div class="fn" />QObject <b><a href="qsignalmapper.html#mapping-4">mapping</a></b> (<i>self</i>, QObject&#160;<i>object</i>)</li><li><div class="fn" /><b><a href="qsignalmapper.html#removeMappings">removeMappings</a></b> (<i>self</i>, QObject&#160;<i>sender</i>)</li><li><div class="fn" /><b><a href="qsignalmapper.html#setMapping">setMapping</a></b> (<i>self</i>, QObject&#160;<i>sender</i>, int&#160;<i>id</i>)</li><li><div class="fn" /><b><a href="qsignalmapper.html#setMapping-2">setMapping</a></b> (<i>self</i>, QObject&#160;<i>sender</i>, QString&#160;<i>text</i>)</li><li><div class="fn" /><b><a href="qsignalmapper.html#setMapping-3">setMapping</a></b> (<i>self</i>, QObject&#160;<i>sender</i>, QWidget&#160;<i>widget</i>)</li><li><div class="fn" /><b><a href="qsignalmapper.html#setMapping-4">setMapping</a></b> (<i>self</i>, QObject&#160;<i>sender</i>, QObject&#160;<i>object</i>)</li></ul><h3>Qt Signals</h3><ul><li><div class="fn" />void <b><a href="qsignalmapper.html#mapped">mapped</a></b> (int)</li><li><div class="fn" />void <b><a href="qsignalmapper.html#mapped-2">mapped</a></b> (const QString&amp;)</li><li><div class="fn" />void <b><a href="qsignalmapper.html#mapped-3">mapped</a></b> (QWidget *)</li><li><div class="fn" />void <b><a href="qsignalmapper.html#mapped-4">mapped</a></b> (QObject *)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QSignalMapper class bundles signals from identifiable
senders.</p>
<p>This class collects a set of parameterless signals, and re-emits
them with integer, string or widget parameters corresponding to the
object that sent the signal.</p>
<p>The class supports the mapping of particular strings or integers
with particular objects using <a href="qsignalmapper.html#setMapping">setMapping</a>(). The objects'
signals can then be connected to the <a href="qsignalmapper.html#map">map</a>() slot which will emit the
<a href="qsignalmapper.html#mapped">mapped</a>() signal with the
string or integer associated with the original signalling object.
Mappings can be removed later using <a href="qsignalmapper.html#removeMappings">removeMappings</a>().</p>
<p>Example: Suppose we want to create a custom widget that contains
a group of buttons (like a tool palette). One approach is to
connect each button's <tt>clicked()</tt> signal to its own custom
slot; but in this example we want to connect all the buttons to a
single slot and parameterize the slot by the button that was
clicked.</p>
<p>Here's the definition of a simple custom widget that has a
single signal, <tt>clicked()</tt>, which is emitted with the text
of the button that was clicked:</p>
<pre class="cpp">
 <span class="keyword">class</span> ButtonWidget : <span class="keyword">public</span> <span class="type"><a href="qwidget.html">QWidget</a></span>
 {
     Q_OBJECT

 <span class="keyword">public</span>:
     ButtonWidget(<span class="type"><a href="qstringlist.html">QStringList</a></span> texts<span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

 <span class="keyword">signals</span>:
     <span class="type">void</span> clicked(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>text);

 <span class="keyword">private</span>:
     <span class="type">QSignalMapper</span> <span class="operator">*</span>signalMapper;
 };
</pre>
<p>The only function that we need to implement is the
constructor:</p>
<pre class="cpp">
 ButtonWidget<span class="operator">.</span>ButtonWidget(<span class="type"><a href="qstringlist.html">QStringList</a></span> texts<span class="operator">,</span> <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
     : <span class="type"><a href="qwidget.html">QWidget</a></span>(parent)
 {
     signalMapper <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QSignalMapper</span>(<span class="keyword">this</span>);

     <span class="type"><a href="qgridlayout.html">QGridLayout</a></span> <span class="operator">*</span>gridLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgridlayout.html">QGridLayout</a></span>;
     <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> texts<span class="operator">.</span>size(); <span class="operator">+</span><span class="operator">+</span>i) {
         <span class="type"><a href="qpushbutton.html">QPushButton</a></span> <span class="operator">*</span>button <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qpushbutton.html">QPushButton</a></span>(texts<span class="operator">[</span>i<span class="operator">]</span>);
         connect(button<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> signalMapper<span class="operator">,</span> SLOT(map()));
         signalMapper<span class="operator">-</span><span class="operator">&gt;</span>setMapping(button<span class="operator">,</span> texts<span class="operator">[</span>i<span class="operator">]</span>);
         gridLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(button<span class="operator">,</span> i <span class="operator">/</span> <span class="number">3</span><span class="operator">,</span> i <span class="operator">%</span> <span class="number">3</span>);
     }

     connect(signalMapper<span class="operator">,</span> SIGNAL(mapped(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>))<span class="operator">,</span>
             <span class="keyword">this</span><span class="operator">,</span> SIGNAL(clicked(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&amp;</span>)));

     setLayout(gridLayout);
 }
</pre>
<p>A list of texts is passed to the constructor. A signal mapper is
constructed and for each text in the list a <a href="qpushbutton.html">QPushButton</a> is created. We connect each
button's <tt>clicked()</tt> signal to the signal mapper's <a href="qsignalmapper.html#map">map</a>() slot, and create a mapping in
the signal mapper from each button to the button's text. Finally we
connect the signal mapper's <a href="qsignalmapper.html#mapped">mapped</a>() signal to the custom
widget's <tt>clicked()</tt> signal. When the user clicks a button,
the custom widget will emit a single <tt>clicked()</tt> signal
whose argument is the text of the button the user clicked.</p>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QSignalMapper" />QSignalMapper.__init__ (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>parent</i>&#160;=&#160;None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a <a href="qsignalmapper.html">QSignalMapper</a> with
parent <i>parent</i>.</p>


<h3 class="fn"><a name="map" />QSignalMapper.map (<i>self</i>)</h3><p>This method is also a Qt slot with the C++ signature <tt>void map()</tt>.</p><p>This slot emits signals based on which object sends signals to
it.</p>


<h3 class="fn"><a name="map-2" />QSignalMapper.map (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>sender</i>)</h3><p>This method is also a Qt slot with the C++ signature <tt>void map(QObject *)</tt>.</p><p>This slot emits signals based on the <i>sender</i> object.</p>


<h3 class="fn"><a name="mapping" /><a href="qobject.html">QObject</a> QSignalMapper.mapping (<i>self</i>, int&#160;<i>id</i>)</h3><p>Returns the sender <a href="qobject.html">QObject</a> that is
associated with the <i>id</i>.</p>
<p><b>See also</b> <a href="qsignalmapper.html#setMapping">setMapping</a>().</p>


<h3 class="fn"><a name="mapping-2" /><a href="qobject.html">QObject</a> QSignalMapper.mapping (<i>self</i>, QString&#160;<i>text</i>)</h3><p>This function overloads <a href="qsignalmapper.html#mapping">mapping</a>().</p>


<h3 class="fn"><a name="mapping-3" /><a href="qobject.html">QObject</a> QSignalMapper.mapping (<i>self</i>, QWidget&#160;<i>widget</i>)</h3><p>This function overloads <a href="qsignalmapper.html#mapping">mapping</a>().</p>
<p>Returns the sender <a href="qobject.html">QObject</a> that is
associated with the <i>widget</i>.</p>


<h3 class="fn"><a name="mapping-4" /><a href="qobject.html">QObject</a> QSignalMapper.mapping (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>object</i>)</h3><p>This function overloads <a href="qsignalmapper.html#mapping">mapping</a>().</p>
<p>Returns the sender <a href="qobject.html">QObject</a> that is
associated with the <i>object</i>.</p>


<h3 class="fn"><a name="removeMappings" />QSignalMapper.removeMappings (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>sender</i>)</h3><p>Removes all mappings for <i>sender</i>.</p>
<p>This is done automatically when mapped objects are
destroyed.</p>


<h3 class="fn"><a name="setMapping" />QSignalMapper.setMapping (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>sender</i>, int&#160;<i>id</i>)</h3><p>Adds a mapping so that when <a href="qsignalmapper.html#map">map</a>() is signalled from the given
<i>sender</i>, the signal mapped(<i>id</i>) is emitted.</p>
<p>There may be at most one integer ID for each sender.</p>
<p><b>See also</b> <a href="qsignalmapper.html#mapping">mapping</a>().</p>


<h3 class="fn"><a name="setMapping-2" />QSignalMapper.setMapping (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>sender</i>, QString&#160;<i>text</i>)</h3><p>Adds a mapping so that when <a href="qsignalmapper.html#map">map</a>() is signalled from the
<i>sender</i>, the signal mapped(<i>text</i> ) is emitted.</p>
<p>There may be at most one text for each sender.</p>


<h3 class="fn"><a name="setMapping-3" />QSignalMapper.setMapping (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>sender</i>, QWidget&#160;<i>widget</i>)</h3><p>Adds a mapping so that when <a href="qsignalmapper.html#map">map</a>() is signalled from the
<i>sender</i>, the signal mapped(<i>widget</i> ) is emitted.</p>
<p>There may be at most one widget for each sender.</p>


<h3 class="fn"><a name="setMapping-4" />QSignalMapper.setMapping (<i>self</i>, <a href="qobject.html">QObject</a>&#160;<i>sender</i>, <a href="qobject.html">QObject</a>&#160;<i>object</i>)</h3><p>Adds a mapping so that when <a href="qsignalmapper.html#map">map</a>() is signalled from the
<i>sender</i>, the signal mapped(<i>object</i> ) is emitted.</p>
<p>There may be at most one object for each sender.</p>
<hr /><h2>Qt Signal Documentation</h2><h3 class="fn"><a name="mapped" />void mapped (int)</h3><p>This is the default overload of this signal.</p><p>This signal is emitted when <a href="qsignalmapper.html#map">map</a>() is signalled from an object that
has an integer mapping set. The object's mapped integer is passed
in <i>i</i>.</p>
<p><b>See also</b> <a href="qsignalmapper.html#setMapping">setMapping</a>().</p>


<h3 class="fn"><a name="mapped-2" />void mapped (const QString&amp;)</h3><p>This signal is emitted when <a href="qsignalmapper.html#map">map</a>() is signalled from an object that
has a string mapping set. The object's mapped string is passed in
<i>text</i>.</p>
<p><b>See also</b> <a href="qsignalmapper.html#setMapping">setMapping</a>().</p>


<h3 class="fn"><a name="mapped-3" />void mapped (QWidget *)</h3><p>This signal is emitted when <a href="qsignalmapper.html#map">map</a>() is signalled from an object that
has a widget mapping set. The object's mapped widget is passed in
<i>widget</i>.</p>
<p><b>See also</b> <a href="qsignalmapper.html#setMapping">setMapping</a>().</p>


<h3 class="fn"><a name="mapped-4" />void mapped (QObject *)</h3><p>This signal is emitted when <a href="qsignalmapper.html#map">map</a>() is signalled from an object that
has an object mapping set. The object provided by the map is passed
in <i>object</i>.</p>
<p><b>See also</b> <a href="qsignalmapper.html#setMapping">setMapping</a>().</p>


<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt&#160;4.9.1 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2011</td><td align="right" width="25%">Qt&#160;4.8.0</td></tr></table></div></address></body></html>